/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using Xceed.Silverlight.Data;

namespace Xceed.Silverlight.ListBox
{
  internal class ScrollToIndexNavigationAction : PanelNavigationActionBase
  {
    internal ScrollToIndexNavigationAction( double scrollManagerInitialTargetIndex, PanelBase panel, NavigationInfo navigationInfo )
      : base( panel, navigationInfo )
    {
      this.ScrollManagerInitialTargetIndex = scrollManagerInitialTargetIndex;

      // Compare the index of first and last fully visible DataPath
      // to determine the real number of fully visible items in viewport
      // to avoid scrolling to much and skip containers.
      PanelBase.PanelLayoutManager layoutManager = this.Panel.LayoutManager;

      DataPath firstVisibleDataPath = layoutManager.GetFirstFullyVisibleDataPath();
      DataPath lastVisibleDataPath = layoutManager.GetLastFullyVisibleDataPath();

      int firstVisibleIndex = layoutManager.ContainerDescriptions.IndexOf( firstVisibleDataPath );
      int lastVisibleIndex = layoutManager.ContainerDescriptions.IndexOf( lastVisibleDataPath );

      // Try to get the last fully visible DataPath in the viewport
      // to know how many containers must be skipped during a page*
      this.FullyVisibleItemsInViewport = ( lastVisibleIndex - firstVisibleIndex );

      // If the container size is very large and only 1 or 2 
      // are fully visible, ensure to at least scroll 1 index
      if( this.FullyVisibleItemsInViewport <= 0 )
      {
        this.FullyVisibleItemsInViewport = 1;
      }
    }

    private double ScrollManagerInitialTargetIndex
    {
      get;
      set;
    }

    public int FullyVisibleItemsInViewport 
    {
      get; 
      set; 
    }

    internal override double CalculateTargetScrollIndex()
    {
      double targetIndex = this.ScrollManagerInitialTargetIndex;
      double directionalScrollingFactor = ( this.NavigationInfo.IsForwardScrolling )
       ? 1
       : -1;

      if( this.NavigationInfo.IsPageScrolling )
      {
        targetIndex += ( this.FullyVisibleItemsInViewport * directionalScrollingFactor );
      }
      else
      {
        targetIndex += directionalScrollingFactor;
      }

      //TODO CN Is there a better way to do this without the ItemCount???
      targetIndex = Math.Min( this.Panel.ParentListBox.DataSourceEnumeratorWrapper.ItemCount - this.Panel.ItemsInViewport, targetIndex );
      return Math.Max( 0, targetIndex );
    }
  }
}
